import { defineStore } from "pinia";
import { BaseInfo, CampusExperience, EducationalExperience, Expertise, HonoraryAwards, InternshipExperience, ProjectExperience, SelfEvaluation } from "../type/typing";
import { reactive, ref } from "vue";

// 从 localStorage 获取初始数据
const getInitialData = () => {
    const savedData = localStorage.getItem('resume_data');
    if (savedData) {
        return JSON.parse(savedData);
    }
    return null;
};

export const useStore = defineStore('global_info', () => {
    const initialData = getInitialData();

    // 如果有保存的数据，使用保存的数据，否则使用默认值
    const base_info = reactive<BaseInfo>(initialData?.base_info || {
        visiable: true,
        content: {
            name: "魏文海",
            gender: "男",
            school: "山东工商学院",
            highest_degree: "学士",
            email: "a642160575@126.com",
            phone: "13863207541"
        },
        index: 0
    });

    const self_evaluation = reactive<SelfEvaluation>(initialData?.self_evaluation || {
        visiable: true,
        content: {
            self_evaluation: "对新事物保持好奇心，喜欢专研，喜欢编程，乐于分享，积极乐观，喜欢迎接挑战。"
        },
        index: 1
    });

    const educational_experience = reactive<EducationalExperience>(initialData?.educational_experience || {
        visiable: true,
        content: {
            educational_experience: [
                {
                    begin_date: new Date(2020, 9, 1),
                    end_date: new Date(2024, 6, 1),
                    attend_school: "山东工商学院",
                    degree: "本科",
                    major: "软件工程",
                    grades: "前5%",
                    major_courses: ["计算机系统基础", "操作系统", "数据库系统原理与应用", "软件需求分析", "软件设计与体系结构", "机器学习", "人工智能"]
                }
            ]
        },
        index: 2
    });

    const expertise = reactive<Expertise>(initialData?.expertise || {
        visiable: true,
        content: {
            expertises: ["Java:熟练掌握Java基础知识、有Java前后端交互开发经验。","Android:掌握Android基础知识，有Android多线程开发经验。","熟悉Python语言，熟悉装饰器、迭代器、生成器表达式，熟悉常见的数据结构如栈、队列、二叉树等。","框架:熟练掌握SpringBoot(Java)，Django Rest Farmework(Python)，Mybatis等主流开发框架，了解Redis原理。","数据库设计:熟练掌握数据库设计工具，有过PowerDesigner使用经验，熟练掌握MySQL数据库以及常见设计规范，优化手段。","数据结构：熟练掌握数据结构基本算法思想、理解物理模型底层存储原理。","需求分析:熟练掌握需求分析书制作，UML图绘画，有需求分析书制作经验。","体系结构设计:良好掌握体系结构设计书制作，各种视图的制作与软件层次划分，有过制作经验。","计算机基础:良好掌握计算机网络、数据结构与算法、了解计算机基本组成原理。"]
        },
        index: 4
    });

    const honorary_awards = reactive<HonoraryAwards>(initialData?.honorary_awards || {
        visiable: true,
        content: {
            honorary_awards: [{
                begin_date: new Date(),
                end_date: new Date(),
                awards: ["奖项1", "奖项2", "奖项3"]
            }]
        },
        index: 5
    });

    const project_experience = reactive<ProjectExperience>(initialData?.project_experience || {
        visiable: true,
        content: {
            project_experience: []
        },
        index: 6
    });

    const internship_experience = reactive<InternshipExperience>(initialData?.internship_experience || {
        visiable: true,
        content: {
            internship_experience: []
        },
        index: 7
    });

    const campus_experience = reactive<CampusExperience>(initialData?.campus_experience || {
        visiable: false,
        content: {
            campus_experience: [
                {
                    begin_date: new Date(),
                    end_date: new Date(),
                    experinece: ["无"],
                }
            ]
        },
        index: 3
    });

    const job_interest = ref<string>(initialData?.job_interest || "后端开发工程师");

    const user_avatar = ref<string>(initialData?.user_avatar || "https://img2.imgtp.com/2024/04/21/gimKm7pN.jpg");

    const template_all = ref<string[]>(initialData?.template_all || ['template01', 'template02']);

    // 保存所有数据到 localStorage
    const saveToLocalStorage = () => {
        const data = {
            base_info,
            self_evaluation,
            educational_experience,
            expertise,
            honorary_awards,
            project_experience,
            internship_experience,
            campus_experience,
            job_interest: job_interest.value,
            user_avatar: user_avatar.value,
            template_all: template_all.value
        };
        localStorage.setItem('resume_data', JSON.stringify(data));
    };

    // 更新函数
    function updateBaseInfo(info: BaseInfo) {
        Object.assign(base_info, info);
        saveToLocalStorage();
    }

    function updateSelfEvaluation(info: SelfEvaluation) {
        Object.assign(self_evaluation, info);
        saveToLocalStorage();
    }

    function updateEducationalExperience(info: EducationalExperience) {
        Object.assign(educational_experience, info);
        saveToLocalStorage();
    }

    function updateExpertise(info: Expertise) {
        Object.assign(expertise, info);
        saveToLocalStorage();
    }

    function updateHonoraryAwards(info: HonoraryAwards) {
        Object.assign(honorary_awards, info);
        saveToLocalStorage();
    }

    function updateProjectExperience(info: ProjectExperience) {
        Object.assign(project_experience, info);
        saveToLocalStorage();
    }

    function updateInternshipExperience(info: InternshipExperience) {
        Object.assign(internship_experience, info);
        saveToLocalStorage();
    }

    function updateCampusExperience(info: CampusExperience) {
        Object.assign(campus_experience, info);
        saveToLocalStorage();
    }

    function updateJobInterest(job: string) {
        job_interest.value = job;
        saveToLocalStorage();
    }

    return {
        template_all,
        base_info,
        user_avatar,
        self_evaluation,
        educational_experience,
        campus_experience,
        expertise,
        honorary_awards,
        project_experience,
        internship_experience,
        job_interest,
        updateJobInterest,
        updateBaseInfo,
        updateSelfEvaluation,
        updateEducationalExperience,
        updateCampusExperience,
        updateExpertise,
        updateHonoraryAwards,
        updateProjectExperience,
        updateInternshipExperience
    };
});